package org.lyl.taobao.api.utils;

import org.lyl.taobao.api.config.DbPoolConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: Re
 * @Date: 2021/3/26 14:12
 */
public class DruidConnectionUtils {
    public Connection getConnection()  {
        DbPoolConnection dbPoolConnection = DbPoolConnection.getInstance();
        Connection connection=null;
        try{
            connection = dbPoolConnection.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭Connection PreparedStatement
     * @param connection
     * @param preparedStatement
     */
    public void closeConnection(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
